package com.sky.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list=new ArrayList<>();
        List<Integer> list1=new ArrayList<>();
        while (begin.isBefore(end)){
            list.add(begin);
            begin=begin.plusDays(1);

        }
        list.add(end);

        for (LocalDate o : list) {
            LocalDateTime beginTime = LocalDateTime.of(o,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(o, LocalTime.MAX);
            list1.add(orderMapper.getTurnoverStatistics(beginTime,endTime));
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(list,","))
                .turnoverList(StringUtils.join(list1,","))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> time=new ArrayList<>();
        List<Integer> newUser=new ArrayList<>();
        List<Integer> allUser=new ArrayList<>();
        while (begin.isBefore(end)){
            time.add(begin);
            begin=begin.plusDays(1);
        }
        time.add(end);
        for (LocalDate o : time) {
            LocalDateTime beginTime = LocalDateTime.of(o,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(o, LocalTime.MAX);
            //获得新增用户数据列表
            newUser.add(userMapper.getNewUserStatistics(beginTime,endTime));
            //获得总用户数据列表
            allUser.add(userMapper.getAllUserStatistics(endTime));

        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(time,","))
                .newUserList(StringUtils.join(newUser,","))
                .totalUserList(StringUtils.join(allUser,","))
                .build();
    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        Map<String,Object> map=new HashMap<>();
        LocalDateTime  beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<Map<String,Object>> list=orderDetailMapper.getTop10(beginTime,endTime);
        List<Object> name = list.stream().map(new Function<Map<String, Object>, Object>() {
            @Override
            public Object apply(Map<String, Object> map) {
                return map.get("name");
            }
        }).collect(Collectors.toList());
        List<Object> number = list.stream().map( m->m.get("num")).collect(Collectors.toList());

        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(name,","))
                .numberList(StringUtils.join(number,","))
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> time=new ArrayList<>();
        List<Integer> OrderCountList=new ArrayList<>();
        List<Integer> validOrderCountList=new ArrayList<>();
        int totalOrderCount=0,f=0;
        int validOrderCount=0,k=0;
        while (begin.isBefore(end)){
            time.add(begin);
            begin=begin.plusDays(1);

        }
        time.add(end);
        for (LocalDate o : time) {
            LocalDateTime beginTime = LocalDateTime.of(o,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(o, LocalTime.MAX);
            f=orderMapper.getCount(null,beginTime,endTime);
            OrderCountList.add(f);
            totalOrderCount+=f;
            k=orderMapper.getCount(Orders.COMPLETED,beginTime,endTime);
            validOrderCountList.add(k);
            validOrderCount+=k;
        }
        Double orderCompletionRate=k*1.0/f;
        return OrderReportVO.builder()
                .dateList(StringUtils.join(time,","))
                .orderCountList(StringUtils.join(OrderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }
}
